package com.oracle.cmp.controller;

import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.PageInfo;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.entity.Customer;
import com.oracle.cmp.service.CustomerService;
import com.oracle.cmp.service.CustomerServiceImpl;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/customer.do")
public class CustomerController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String op = request.getParameter("op");
        if("query".equals(op)){
            query(request,response);
        }else if("delete".equals(op)){
            delete(request,response);
        }else if("edit".equals(op)){
            edit(request,response);
        }else if("update".equals(op)){
            update(request,response);
        }else if("add".equals(op)){
            add(request,response);
        }else if("save".equals(op)){
            save(request,response);
        }

    }

    public void query(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        CustomerService customerService = new CustomerServiceImpl();
        String customerName = request.getParameter("customerName");
        String pageNo = request.getParameter("pageNum");
        int pageNum = 1;
        if(pageNo!=null)
            pageNum = Integer.parseInt(pageNo);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("customerName",customerName);
        Result result = customerService.queryForPage(map,pageNum);
        int total = customerService.count(map);
        int pages = (total+ Constant.PAGESIZE-1)/Constant.PAGESIZE;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList((List)result.getData());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(Constant.PAGESIZE);
        pageInfo.setTotal(total);
        pageInfo.setPages(pages);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("customerName", customerName);
        request.getRequestDispatcher("/page/system/customer/customerlist.jsp").forward(request,response);
    }
    /**
     * 新增页面跳转
     * @return
     */
    public void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/page/system/customer/customeradd.jsp").forward(request,response);
    }

    /**
     * 新增保存
     * @return
     */

    public void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        CustomerService customerService = new CustomerServiceImpl();
        String name = request.getParameter("name");
        String tel = request.getParameter("tel");
        String carno = request.getParameter("carno");
        String cartype = request.getParameter("cartype");
        Customer customer = new Customer();
        customer.setName(name);
        customer.setTel(tel);
        customer.setCarno(carno);
        customer.setCartype(cartype);
        customer.setCreatedate(new Date());
        customerService.save(customer);
        response.sendRedirect(request.getContextPath()+"/customer.do?op=query");
    }
    /**
     * 删除客户

     * @return
     */

    public void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        CustomerService customerService = new CustomerServiceImpl();
        String id = request.getParameter("id");
        customerService.delete(Integer.parseInt(id));
        request.getRequestDispatcher("/customer.do?op=query").forward(request,response);
    }
    /**
     * 进入客户更新页面
     * @return
     */
    public void edit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        CustomerService customerService = new CustomerServiceImpl();
        String id = request.getParameter("id");
        Result result = customerService.selectById(Integer.parseInt(id));
        request.setAttribute("c", result.getData());
        request.getRequestDispatcher("/page/system/customer/customeredit.jsp").forward(request,response);
    }
    /**
     * 客户更新
     * @return
     */
    public void update(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        CustomerService customerService = new CustomerServiceImpl();
        String name = request.getParameter("name");
        String tel = request.getParameter("tel");
        String carno = request.getParameter("carno");
        String cartype = request.getParameter("cartype");
        String id = request.getParameter("id");
        Customer customer = new Customer();
        customer.setName(name);
        customer.setTel(tel);
        customer.setCarno(carno);
        customer.setCartype(cartype);
        customer.setId(Integer.parseInt(id));
        customerService.update(customer);
        response.sendRedirect(request.getContextPath()+"/customer.do?op=query");
    }
}
